package com.skyline.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.type.Activity;
import com.skyline.base.type.IdolType;
import com.skyline.common.bean.Page;
import com.skyline.user.dao.FanDao;
import com.skyline.user.model.Fan;
import com.skyline.user.service.AttentionService;

@Service("attentionService")
public class AttentionServiceImpl implements AttentionService {

	@Autowired
	private FanDao fandao;

	@Override
	public void payAttention(Long idolId, String idolNickname,
			String idolPortrait, Long fanId, String fanNickname,
			String fanPortrait, IdolType idolType) {

		if (fandao.existAttention(idolId, fanId, Activity.DELETED)) {
			fandao.recoverAttention(idolId, fanId);
		} else {
			fandao.insertIdolAndFans(idolId, idolNickname, idolPortrait, fanId,
					fanNickname, fanPortrait, idolType, false);
		}
		if (idolType == IdolType.USER) {
			if (fandao.existAttention(fanId, idolId, Activity.NORMAL)) {
				fandao.updateAttentionType(idolId, fanId, true);
				fandao.updateAttentionType(fanId, idolId, true);
			}
		}

	}

	@Override
	public List<Fan> queryAttentionByType(Long fanId, IdolType idolType,
			Page page) {
		return fandao.queryIdolsByFansIdAndIdolType(fanId, idolType, page,
				Activity.NORMAL);
	}

	@Override
	public List<Fan> queryAttentionByType(Long fanId, IdolType idolType) {
		Page page = new Page();
		page.setSize(10);
		return fandao.queryIdolsByFansIdAndIdolType(fanId, idolType, page,
				Activity.NORMAL);
	}

	@Override
	public List<Fan> queryFans(Long idolId, Page page) {
		return fandao.queryFansByIdolId(idolId, page, Activity.NORMAL);
	}

	@Override
	public int countFans(Long idolId) {
		return fandao.countFansByIdolId(idolId);
	}

	@Override
	public int countAttentionByType(Long fanId, IdolType type) {
		return fandao.countIdolByFansIdAndIdolType(fanId, type);
	}

	@Override
	public boolean isFan(Long idolId, Long fanId) {
		return fandao.existAttention(idolId, fanId, Activity.NORMAL);
	}

	@Override
	public boolean isFanEachOther(Long idolId, Long fanId) {
		if (fandao.existAttention(idolId, fanId, Activity.NORMAL)
				&& fandao.existAttention(fanId, idolId, Activity.NORMAL))
			return true;
		return false;
	}

	@Override
	public void cancelAttention(Long idolId, Long fanId) {
		fandao.deleteIdol(idolId, fanId);
		fandao.updateAttentionType(idolId, fanId, false);
		fandao.updateAttentionType(fanId, idolId, false);

	}

}